<?php

/**
 * This is the model class for table "product".
 *
 * The followings are the available columns in table 'product':
 * @property integer $pro_id
 * @property string $pro_name
 * @property integer $pro_price
 * @property integer $pro_cat_id
 * @property integer $pro_date_create
 * @property integer $pro_sale
 * @property string $pro_avatar
 * @property string $pro_des
 * @property integer $pro_active
 */
class Product extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'product';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('pro_name', 'required'),
			array('pro_price, pro_cat_id, pro_special, pro_date_create, pro_sale, pro_active', 'numerical', 'integerOnly'=>true),
			array('pro_name, pro_code, pro_avatar', 'length', 'max'=>255),
			array('pro_des', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('pro_id, pro_name, pro_price, pro_cat_id, pro_special, pro_date_create, pro_sale, pro_avatar, pro_des, pro_active', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
         'category' => array(self::BELONGS_TO,'Category','pro_cat_id'),
         'images' => array(self::HAS_MANY,'ProImage','pro_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'pro_id' => 'ID',
         'pro_code' => 'Mã sản phẩm',
			'pro_name' => 'Tên sản phẩm',
			'pro_price' => 'Giá',
			'pro_cat_id' => 'Danh mục',
			'pro_date_create' => 'Ngày tạo',
			'pro_sale' => 'Giảm giá (%)',
         'pro_special' => 'SP nổi bậT',
			'pro_avatar' => 'Ảnh đại diện',
			'pro_des' => 'Mô tả',
			'pro_active' => 'Kích hoạt',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('pro_id',$this->pro_id);
      $criteria->compare('pro_code',$this->pro_code);
		$criteria->compare('pro_name',$this->pro_name,true);
		$criteria->compare('pro_price',$this->pro_price);
		$criteria->compare('pro_cat_id',$this->pro_cat_id);
		$criteria->compare('pro_date_create',$this->pro_date_create);
		$criteria->compare('pro_sale',$this->pro_sale);
		$criteria->compare('pro_avatar',$this->pro_avatar,true);
		$criteria->compare('pro_des',$this->pro_des,true);
		$criteria->compare('pro_active',$this->pro_active);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
